package com.xwj.juc.queue;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 缓存元素节点
 *
 * @author xwj
 * @date 2020/3/12
 */
public class Element<K> implements Delayed {
    private K k;
    /**
     * 过期时间
     */
    private Long expireTime;

    public K getK() {
        return k;
    }

    public void setK(K k) {
        this.k = k;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expireTime - System.currentTimeMillis(), unit);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }
}
